package com.cuijq.ms.compose.v3;

import com.cuijq.ms.compose.v3.repo.DepartmentRepo;
import com.cuijq.ms.compose.v3.repo.EmployeeRepo;
import java.util.List;

/**
 * @author by CuiJQ
 * @Classname Demo
 * @Description Demo Description
 * @Date 2021/5/26 23:39
 */
public class Demo {

  private static final long ORGANIZATION_ROOT_ID = 1001;
  private DepartmentRepo departmentRepo;

  // 依赖注入
  private EmployeeRepo employeeRepo;

  // 依赖注入
  public void buildOrganization() {
    Department rootDepartment = new Department(ORGANIZATION_ROOT_ID);
    buildOrganization(rootDepartment);
  }

  private void buildOrganization(Department department) {
    //部门
    List<Long> subDepartmentIds = departmentRepo.getSubDepartmentIds(department.getId());
    for (Long subDepartmentId : subDepartmentIds) {
      Department subDepartment = new Department(subDepartmentId);
      department.addSubNode(subDepartment);
      buildOrganization(subDepartment);
    }
    //部门人员
    List<Long> employeeIds = employeeRepo.getDepartmentEmployeeIds(department.getId());
    for (Long employeeId : employeeIds) {
      double salary = employeeRepo.getEmployeeSalary(employeeId);
      department.addSubNode(new Employee(employeeId, salary));
    }
  }
}
